package com.example.administrator.food.modle.home.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.example.administrator.food.R;
import com.example.administrator.food.modle.home.adapter.XbannerAdapter;
import com.example.administrator.food.modle.home.bean.Home;
import com.example.administrator.food.uil.UrlConfigs;
import com.example.administrator.food.utils.BaseApp;
import com.google.gson.Gson;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements View.OnClickListener{
    private ListView listView;
    private ViewPager viewpager;
    private XbannerAdapter adapter;
    private StringRequest request;
    private List<Home.DatasBean.BannerBean>data;

    public HomeFragment() {
        // Required empty public constructor
    }

    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View view= inflater.inflate(R.layout.fragment_home, container, false);
         loadData();
        initView(view);
        //实例化适配器
        adapter = new XbannerAdapter(getContext(),data,"");
        //设置适配器
        viewpager.setAdapter(adapter);



        return view;
    }

    private void initView(View view) {
        viewpager=(ViewPager) view.findViewById(R.id.fragment_home_viewpager);
       viewpager.setOnClickListener(this);

    }

    private void loadData() {
        request = new StringRequest(
                UrlConfigs.HOME_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.e("TAG", "onResponse: "+response );
                        Gson gson = new Gson();
                        Home home = gson.fromJson(response, Home.class);
                        List<Home.DatasBean.BannerBean> banners = home.getDatas().getBanner();
                        data.addAll(banners);
                        Log.e("xxxxxx", "onResponse: "+banners.size() );
                        adapter.notifyDataSetChanged();

                    }
                },
                null
        );
        //3.设置请求取消标记
        request.setTag("banner");
        //4.将请求添加在请求队列
        BaseApp.getMyApp().getRequestQueue().add(request);
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        BaseApp.getMyApp().getRequestQueue().cancelAll("banner");
        super.onDestroy();
    }
}
